//
//  XMPicwordDetailViewController.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/7/2.
//

#import "XMPicwordDetailViewController.h"
#import "XMPicwordDetailView.h"
#import "XMNetwork+find.h"
#import "XMNetwork+mine.h"

#import "XMPicwordCommentModel.h"

@interface XMPicwordDetailViewController ()<XMPicwordDetailViewDelegate>

@property (nonatomic, strong) XMPicwordDetailView *detailView;

@property (nonatomic, assign) NSInteger page;

@end

@implementation XMPicwordDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatUI];
}

- (void)creatUI {
    [self.view addSubview:self.detailView];
    _page = 1;
    dispatch_group_t downloadGroup = dispatch_group_create();
    dispatch_group_enter(downloadGroup);
    [self getPicwordDetailInfoWithDownloadGroup:downloadGroup];
    dispatch_group_enter(downloadGroup);
    [self getPicwordCommentWithDownloadGroup:downloadGroup];
    XMWeakSelf;
    dispatch_group_notify(downloadGroup, dispatch_get_main_queue(), ^{
        [weakSelf.detailView loadSuccess];
    });
}

//详情
- (void)getPicwordDetailInfoWithDownloadGroup:(dispatch_group_t)downloadGroup {
    XMWeakSelf;
    [_Network getPicwordDetailWithPicwordId:_picwordId
                                    success:^(id  _Nonnull responseObject) {
        if (downloadGroup) {
            dispatch_group_leave(downloadGroup);
        }
        XMPicwordDetailModel *detailModel = [XMPicwordDetailModel yy_modelWithJSON:[responseObject objectForKey:@"data"]];
        [weakSelf.detailView setContentWithDetailModel:detailModel];
    } failure:^(NSError * _Nonnull error) {
        if (downloadGroup) {
            dispatch_group_leave(downloadGroup);
        }
    }];
}

//获取评论列表
- (void)getPicwordCommentWithDownloadGroup:(dispatch_group_t)downloadGroup {
    XMWeakSelf;
    [_Network getPicwordCommentWithPicwordId:_picwordId
                                        page:[NSString stringWithFormat:@"%ld", _page]
                                     success:^(id  _Nonnull responseObject) {
        if (downloadGroup) {
            dispatch_group_leave(downloadGroup);
        }
        NSArray *commentModelArr = [NSArray yy_modelArrayWithClass:[XMPicwordCommentModel class] json:[responseObject[@"data"] safeArrayForKey:@"records"]];
        NSString *totalNum = [responseObject[@"data"] safeStringForKey:@"total"];
        [weakSelf.detailView setContentWithCommentModelArr:commentModelArr
                                                  totalNum:totalNum
                                                      page:weakSelf.page];
        if (!downloadGroup) {
            [weakSelf.detailView loadSuccess];
        }
    } failure:^(NSError * _Nonnull error) {
        if (downloadGroup) {
            dispatch_group_leave(downloadGroup);
        }
    }];
}

//评论图文
- (void)picwordCommentWithContent:(NSString *)contentStr
                        commentId:(NSString *)commentId
                     commentLevel:(NSString *)commentLevel {
    XMWeakSelf;
    [_Network picwordCommentWithPicwordId:_picwordId
                                commentId:commentId
                           picwordContent:contentStr
                             commentLevel:commentLevel
                                  success:^(id  _Nonnull responseObject) {
        [weakSelf refreshData];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

#pragma mark - Delegate
//发表评论
- (void)commentPublishWithContent:(NSString *)commentStr
                        commentId:(NSString *)commentId
                     commentLevel:(NSString *)commentLevel {
    [self picwordCommentWithContent:commentStr
                          commentId:commentId
                       commentLevel:commentLevel];
}

//点赞
- (void)picwordLikeAction {
    XMWeakSelf;
    [_Network picwordLikeWithPicwordId:_picwordId
                               success:^(id  _Nonnull responseObject) {
        [weakSelf.detailView picwordLikeSuccess];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

//评论点赞
- (void)commentLikeWithCommentId:(NSString *)commentId {
    XMWeakSelf;
    [_Network picwordCommentLikeWithCommentId:commentId
                                      success:^(id  _Nonnull responseObject) {
        [weakSelf refreshData];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

//关注
- (void)picwordAttentionWithUserId:(NSString *)userId
                       isAttention:(BOOL)isAttention {
    XMWeakSelf;
    if (isAttention) {
        [_Network cancelAttentionWithUserId:userId
                                    success:^(id  _Nonnull responseObject) {
            [weakSelf.detailView attentionSuccess:!isAttention];
        } failure:^(NSError * _Nonnull error) {
            
        }];
    }
    else {
        [_Network friendAttentionWithUserId:userId
                                    success:^(id  _Nonnull responseObject) {
            [weakSelf.detailView attentionSuccess:!isAttention];
        } failure:^(NSError * _Nonnull error) {
            
        }];
    }
}

- (void)refreshData {
    _page = 1;
    [self getPicwordCommentWithDownloadGroup:nil];
}

- (void)loadMore {
    _page += 1;
    [self getPicwordCommentWithDownloadGroup:nil];
}

- (XMPicwordDetailView *)detailView {
    if (!_detailView) {
        _detailView = [[XMPicwordDetailView alloc] initWithFrame:self.view.bounds];
        _detailView.delegate = self;
    }
    return _detailView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
